using System;

namespace TSharp.Core.Util.TypeConvert
{
    /// <summary>
    /// 
    /// </summary>
    public class NullableInt32TypeDetectorImpl : NullableValueTypeDetector<int>
    {
        /// <summary>
        /// Strings the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? String(string detectorObj, int? arg)
        {
            int result;
            bool parseSucceed = int.TryParse(detectorObj, out result);
            return parseSucceed ? result : arg;
        }

        /// <summary>
        /// Int32s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override Int32? Int32(Int32 detectorObj, Int32? arg)
        {
            return detectorObj;
        }

        /// <summary>
        /// Decimals the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Decimal(decimal detectorObj, int? arg)
        {
            return Convert.ToInt32(detectorObj);
        }

        /// <summary>
        /// Int16s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Int16(short detectorObj, int? arg)
        {
            return detectorObj;
        }

        /// <summary>
        /// Int64s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Int64(long detectorObj, int? arg)
        {
            return Convert.ToInt32(detectorObj);
        }

        /// <summary>
        /// Bytes the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Byte(byte detectorObj, int? arg)
        {
            return Convert.ToInt32(detectorObj);
        }

        /// <summary>
        /// Booleans the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">if set to <c>true</c> [detector obj].</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Boolean(bool detectorObj, int? arg)
        {
            return Convert.ToInt32(detectorObj);
        }

        /// <summary>
        /// Chars the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override int? Char(char detectorObj, int? arg)
        {
            return Convert.ToInt32(detectorObj);
        }

        /// <summary>
        /// Byteses the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="TArg">The T arg.</param>
        /// <returns></returns>
        public override int? Bytes(byte[] detectorObj, int? TArg)
        {
            return Convert.ToInt32(detectorObj);
        }
    }
}